/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.push.web;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.foreveross.bsl.system.application.ApplicationRegisterApplication;

/**
 * xxx
 *
 * @author Wangyi
 * @version v1.0
 *
 * @date 2013-8-27
 *
 */
@Component
public class AppIdKeyHelper {
	
	@Inject
	private ApplicationRegisterApplication appRegClient;
	
	public AppIdKeyPair check(String appId, String appKey){
		return this.check(appId, appKey, true);
	}
	
	public AppIdKeyPair check(String appId, String appKey, boolean bothEmptyException){
		if(StringUtils.isEmpty(appKey) && !StringUtils.isEmpty(appId)){
			appKey=appRegClient.getAppKeyByAppId(appId);
			if(appKey==null){
				throw new IllegalArgumentException("通过appId："+appId+"获取的appKey为空");
			}
		}
		else if(StringUtils.isEmpty(appId) && !StringUtils.isEmpty(appKey)){
			appId=this.appRegClient.getAppIdByAppKey(appKey);
			if(appId==null){
				throw new IllegalArgumentException("通过appKey："+appKey+"获取的appId为空");
			}
		}
		else if(StringUtils.isEmpty(appId) && StringUtils.isEmpty(appKey) && bothEmptyException){
			throw new IllegalArgumentException("输入的appId和appKey不能都为空");
		}
		return new AppIdKeyPair(appId, appKey);
	}
	
	public static class AppIdKeyPair{
		private final String appId;
		private final String appKey;
		
		public AppIdKeyPair(String appId, String appKey){
			this.appId=appId;
			this.appKey=appKey;
		}

		/**
		 * @return the appId
		 */
		public String getAppId() {
			return appId;
		}

		/**
		 * @return the appKey
		 */
		public String getAppKey() {
			return appKey;
		}
		
	}
}
